package controller

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"strconv"
	"woanju/dao/mysql"
	"woanju/logic"
	"woanju/models"
)

func EquipmentHandler(c *gin.Context) {
	//1.参数校验
	p := new(models.ParamEquipmentInfo)
	if err := c.ShouldBindJSON(&p); err != nil {
		//请求参数有误，直接返回响应
		zap.L().Error("EquipmentInfo with invalid param", zap.Error(err))
		//判断err是不是validator.ValidationErrors类型
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			ResponseError(c, CodeInvalidParam)
			return
		}

		//c.JSON(http.StatusOK, gin.H{
		//	"msg": removeTopStruct(errs.Translate(trans)), //翻译错误
		//})
		ResponseErrorWithMsg(c, CodeInvalidParam, removeTopStruct(errs.Translate(trans)))

		return
	}

	uid, _, err := GetCurrentUser(c)
	if err != nil {
		ResponseError(c, CodeNeedLogin)
		return
	}

	e := &models.Equipment{
		EquipmentName: p.EquipmentName,
		Type:          p.Type,
		Status:        p.Status,
		UserID:        uid,
	}

	//2.业务处理
	if err := logic.InsertEquipment(e); err != nil {
		zap.L().Error("logic.InsertEquipment failed", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}

	//3.返回响应
	ResponseSuccess(c, nil)
}

func DeleteEquipmentHandler(c *gin.Context) {
	//1.参数处理
	var eid string
	eid = c.Param("id")
	id, _ := strconv.ParseInt(eid, 10, 64)

	//2.业务处理
	if err := logic.DeleteEquipment(id); err != nil {
		zap.L().Error("logic.DeleteEquipment failed", zap.Error(err))
		if errors.Is(err, mysql.ErrorEquipmentNotExist) {
			ResponseErrorWithMsg(c, CodeInvalidParam, "该设备不存在")
			return
		}

		ResponseError(c, CodeServerBusy)
		return
	}

	//3.返回响应
	ResponseSuccess(c, nil)
}

func UpdateStatusEquipmentHandler(c *gin.Context) {
	//1.参数处理
	p := new(models.ParamEquipmentStatus)
	if err := c.ShouldBindJSON(&p); err != nil {
		//请求参数有误，直接返回响应
		zap.L().Error("UpdateEquipmentStatus with invalid param", zap.Error(err))
		//判断err是不是validator.ValidationErrors类型
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			ResponseError(c, CodeInvalidParam)
			return
		}

		//c.JSON(http.StatusOK, gin.H{
		//	"msg": removeTopStruct(errs.Translate(trans)), //翻译错误
		//})
		ResponseErrorWithMsg(c, CodeInvalidParam, removeTopStruct(errs.Translate(trans)))

		return
	}

	//2业务处理
	if err := logic.UpdateEquipmentStatus(p); err != nil {
		zap.L().Error("logic.UpdateEquipmentStatus failed", zap.Error(err))
		if errors.Is(err, mysql.ErrorEquipmentNotExist) {
			ResponseError(c, CodeEquipmentNotExist)
			return
		}
		if errors.Is(err, mysql.ErrorRepeated) {
			ResponseError(c, CodeRepeated)
			return
		}
		ResponseError(c, CodeServerBusy)
		return
	}

	//3.返回响应
	ResponseSuccess(c, nil)
}

func EquipmentListHandler(c *gin.Context) {
	//1.参数校验
	uid, _, err := GetCurrentUser(c)
	if err != nil {
		ResponseError(c, CodeNeedLogin)
		return
	}

	//2.逻辑处理
	eList, err := logic.GetEquipmentList(uid)
	if err != nil {
		zap.L().Error("logic.GetEquipmentList failed", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}

	//3.返回响应
	ResponseSuccess(c, eList)
}
